/**
 * 初始化相机管理页面
 */
function initCameraPage() {
    // 初始化相机表格
    initTable('cameraTable', '/api/cameras', [
        { field: 'id', title: 'ID' },
        { field: 'name', title: '相机名称' },
        {
            field: 'typeName',
            title: '相机类型',
            render: (item) => item.typeName || '未知'
        },
        { field: 'brand', title: '品牌' },
        { field: 'price', title: '价格' },
        {
            title: '操作',
            render: (item) => `
                <button class="btn btn-warning" onclick="editCamera(${item.id})">编辑</button>
                <button class="btn btn-danger" onclick="deleteCamera(${item.id})">删除</button>
            `
        }
    ]);

    // 初始化相机类型下拉框
    const cameraTypeSelect = document.getElementById('cameraTypeId');
    if (cameraTypeSelect) {
        ajaxRequest('/api/cameraTypes', 'GET', null,
            (types) => {
                cameraTypeSelect.innerHTML = '<option value="">请选择相机类型</option>';
                types.forEach(type => {
                    const option = document.createElement('option');
                    option.value = type.id;
                    option.textContent = type.name;
                    cameraTypeSelect.appendChild(option);
                });
            },
            (error) => {
                alert('获取相机类型失败: ' + error.message);
            }
        );
    }

    // 绑定添加相机表单提交事件
    const addForm = document.getElementById('addCameraForm');
    if (addForm) {
        addForm.addEventListener('submit', function(e) {
            e.preventDefault();
            if (validateForm(this)) {
                const formData = new FormData(this);
                const cameraData = Object.fromEntries(formData.entries());

                ajaxRequest('/api/cameras', 'POST', cameraData,
                    () => {
                        alert('添加成功');
                        hideModal('addCameraModal');
                        initCameraPage();
                        this.reset();
                    },
                    (error) => {
                        alert('添加失败: ' + error.message);
                    }
                );
            }
        });
    }
}

/**
 * 编辑相机
 * @param {number} id - 相机ID
 */
function editCamera(id) {
    ajaxRequest(`/api/cameras/${id}`, 'GET', null,
        (camera) => {
            const editForm = document.getElementById('editCameraForm');
            if (editForm) {
                for (const key in camera) {
                    const input = editForm.querySelector(`[name="${key}"]`);
                    if (input) {
                        input.value = camera[key];
                    }
                }
                showModal('editCameraModal');
            }
        },
        (error) => {
            alert('获取相机信息失败: ' + error.message);
        }
    );
}

/**
 * 删除相机
 * @param {number} id - 相机ID
 */
function deleteCamera(id) {
    if (confirm('确定要删除这个相机吗？')) {
        ajaxRequest(`/api/cameras/${id}`, 'DELETE', null,
            () => {
                alert('删除成功');
                initCameraPage();
            },
            (error) => {
                alert('删除失败: ' + error.message);
            }
        );
    }
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', initCameraPage);
